package com.openbravo.pos.panels;


import cn.ekuma.epos.businesslogic.ProductUtil;
import cn.ekuma.epos.businesslogic.RoleUtil;
import cn.ekuma.epos.core.util.ProductSwingUtil;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.swing.dialog.AbstractKeyBoardJDialog;

import com.openbravo.bean.AppUser;
import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.editor.JEditorStringNumber;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppConfig;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.pos.panels.link.JProductStorePanel;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;

public class JProductDetail extends AbstractKeyBoardJDialog {
    Product obj;
    I_DataLogicSales dlSales1;
    I_DataLogicSystem dlSystem1;
	JProductStorePanel jProductStorePanel;
	boolean showBuyPrice;
	AppUser user1;
	private String priceGroup;
	
	 /** Creates new form JProductDetail */
    private JProductDetail(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    /** Creates new form JProductDetail 
     * @wbp.parser.constructor*/
    private JProductDetail(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }    
    
    private void init(I_DataLogicSales dlSales,I_DataLogicSystem dlSystem,AppView app) {
    	 priceGroup=app.getProperties().getProperty(AppConfig.SALE_PRODUCT_GROUPNAME);
    	 m_jtxtBarCode.addEditorKeys(m_jNumberKeys);
		 jProductStorePanel=new JProductStorePanel(dlSales);
		 jPanel1.add(jProductStorePanel, BorderLayout.CENTER);
		 
		 this.dlSystem1=dlSystem;
		 this.dlSales1=dlSales;
		 this.user1=(AppUser) app.getAppUserView().getUser();
		 
		 showBuyPrice=false;
		 
		 KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_F9, 0);
			ActionListener actionListener = new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent actionEvent) {
					showBuyPrice = RoleUtil.hasPermission(getRootPane(), user1, dlSystem1,RoleUtil.Permission_EDIT_TICKET_LINE);
					updateProduct();
				}
			};
			getRootPane().registerKeyboardAction(actionListener, stroke,
					JComponent.WHEN_IN_FOCUSED_WINDOW);
			
			m_jtxtBarCode.activate();	
    }
    

    public static JProductDetail getInstance(Component parent, I_DataLogicSales dlSales,I_DataLogicSystem dlSystem,AppView app){
        Window window = getWindow(parent);
        JProductDetail myMsg;
        if (window instanceof Frame) {
            myMsg = new JProductDetail((Frame) window, true);
        } else {
            myMsg = new JProductDetail((Dialog) window, true);
        }
        myMsg.init(dlSales,dlSystem,app);
        return myMsg;
    }
    
	private void initComponents() {
		jPanel2 = new javax.swing.JPanel();
		jPanel5 = new javax.swing.JPanel();
		jPanel5.setPreferredSize(new Dimension(10, 200));
		jPanel1 = new javax.swing.JPanel();
		m_jtxtBarCode = new JEditorStringNumber();
        m_jEnter = new javax.swing.JButton();

		jPanel2.setLayout(new java.awt.BorderLayout());

		jPanel5.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5,
				5));

		jPanel2.add(jPanel5, BorderLayout.NORTH);
		jPanel5.setLayout(null);

		JLabel label = new JLabel();
		label.setText(AppLocal.getIntString("label.prodname"));
		label.setBounds(10, 10, 118, 15);
		jPanel5.add(label);

		m_jName = new JTextField();
		m_jName.setEditable(false);
		m_jName.setBounds(138, 7, 276, 21);
		jPanel5.add(m_jName);

		jMnemonic = new JTextField();
		jMnemonic.setEditable(false);
		jMnemonic.setBounds(412, 7, 69, 21);
		jPanel5.add(jMnemonic);

		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.prodref"));
		label_1.setBounds(10, 41, 118, 15);
		jPanel5.add(label_1);

		m_jRef = new JTextField();
		m_jRef.setEditable(false);
		m_jRef.setBounds(138, 38, 110, 21);
		jPanel5.add(m_jRef);

		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.prodbarcode"));
		label_2.setBounds(10, 76, 118, 15);
		jPanel5.add(label_2);

		m_jCode = new JTextField();
		m_jCode.setEditable(false);
		m_jCode.setBounds(138, 73, 110, 21);
		jPanel5.add(m_jCode);

		JLabel label_3 = new JLabel();
		label_3.setText(AppLocal.getIntString("label.prodpricebuy"));
		label_3.setBounds(280, 76, 95, 15);
		jPanel5.add(label_3);

		m_jPriceBuy = new JTextField();
		m_jPriceBuy.setEditable(false);
		m_jPriceBuy.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jPriceBuy.setBounds(385, 73, 80, 21);
		jPanel5.add(m_jPriceBuy);

		JLabel label_4 = new JLabel();
		label_4.setText(AppLocal.getIntString("label.prodpricesell"));
		label_4.setBounds(280, 104, 95, 15);
		jPanel5.add(label_4);

		m_jPriceSell = new JTextField();
		m_jPriceSell.setEditable(false);
		m_jPriceSell.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jPriceSell.setBounds(385, 101, 80, 21);
		jPanel5.add(m_jPriceSell);

		JLabel label_5 = new JLabel();
		label_5.setText(AppLocal.getIntString("label.baseCustomerPrice"));
		label_5.setBounds(280, 134, 95, 15);
		jPanel5.add(label_5);

		m_jCustomerPrice = new JTextField();
		m_jCustomerPrice.setEditable(false);
		m_jCustomerPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		m_jCustomerPrice.setBounds(385, 131, 80, 21);
		jPanel5.add(m_jCustomerPrice);

		JLabel label_6 = new JLabel();
		label_6.setText(AppLocal.getIntString("label.prodcut.m_sManufacturer"));
		label_6.setBounds(10, 101, 118, 20);
		jPanel5.add(label_6);

		jm_sManufacturer = new JTextField();
		jm_sManufacturer.setEditable(false);
		jm_sManufacturer.setBounds(138, 101, 110, 21);
		jPanel5.add(jm_sManufacturer);

		JLabel label_7 = new JLabel();
		label_7.setText(AppLocal.getIntString("label.product.origin"));
		label_7.setBounds(10, 131, 118, 20);
		jPanel5.add(label_7);

		jm_origin = new JTextField();
		jm_origin.setEditable(false);
		jm_origin.setHorizontalAlignment(SwingConstants.CENTER);
		jm_origin.setBounds(138, 131, 110, 21);
		jPanel5.add(jm_origin);

		JLabel label_8 = new JLabel();
		label_8.setText(AppLocal.getIntString("label.product.m_iDurability"));
		label_8.setBounds(10, 161, 110, 20);
		jPanel5.add(label_8);

		jm_iDurability = new JTextField();
		jm_iDurability.setEditable(false);
		jm_iDurability.setHorizontalAlignment(SwingConstants.CENTER);
		jm_iDurability.setBounds(138, 161, 110, 21);
		jPanel5.add(jm_iDurability);

		JLabel label_9 = new JLabel();
		label_9.setText(AppLocal.getIntString("label.prodaux"));
		label_9.setBounds(280, 164, 101, 15);
		jPanel5.add(label_9);

		m_jComment = new JCheckBox();
		m_jComment.setEnabled(false);
		m_jComment.setBounds(398, 161, 80, 21);
		jPanel5.add(m_jComment);
		
		jm_quantity = new JTextField();
		jm_quantity.setEditable(false);
		jm_quantity.setHorizontalAlignment(SwingConstants.RIGHT);
		jm_quantity.setBounds(280, 38, 70, 21);
		jPanel5.add(jm_quantity);
		
		jm_unit = new JTextField();
		jm_unit.setEditable(false);
		jm_unit.setHorizontalAlignment(SwingConstants.CENTER);
		jm_unit.setBounds(385, 38, 80, 21);
		jPanel5.add(jm_unit);

		jPanel1.setLayout(new java.awt.BorderLayout());

		jPanel2.add(jPanel1, BorderLayout.CENTER);

		getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 665) / 2, (screenSize.height - 565) / 2,
				727, 565);

		panel = new JPanel();
		getContentPane().add(panel, BorderLayout.EAST);
		panel.setLayout(new BorderLayout(0, 0));

		m_jPanEntries = new javax.swing.JPanel();
		panel.add(m_jPanEntries, BorderLayout.NORTH);
		m_jNumberKeys = new JEditorKeys();
		jPanel9 = new javax.swing.JPanel();
		m_jPanEntries.setLayout(new BoxLayout(m_jPanEntries, BoxLayout.Y_AXIS));
		m_jPanEntries.add(m_jNumberKeys);

		jPanel9.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5,
				5));
		m_jtxtBarCode.setBackground(java.awt.Color.white);
		m_jtxtBarCode.setBorder(javax.swing.BorderFactory.createCompoundBorder(
				javax.swing.BorderFactory
						.createLineBorder(javax.swing.UIManager.getDefaults()
								.getColor("Button.darkShadow")),
				javax.swing.BorderFactory.createEmptyBorder(1, 4, 1, 4)));
		m_jtxtBarCode.setOpaque(true);
		m_jtxtBarCode.setPreferredSize(new java.awt.Dimension(100, 22));

		m_jEnter.setIcon(new javax.swing.ImageIcon(getClass().getResource(
				"/com/openbravo/images/barcode.png"))); // NOI18N
		m_jEnter.setFocusPainted(false);
		m_jEnter.setFocusable(false);
		m_jEnter.setRequestFocusEnabled(false);
		m_jEnter.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				m_jEnterActionPerformed(evt);
			}
		});
		jPanel9.setLayout(new BoxLayout(jPanel9, BoxLayout.X_AXIS));
		jPanel9.add(m_jtxtBarCode);
		jPanel9.add(m_jEnter);
		m_jPanEntries.add(jPanel9);

		btnNewButton = new JButton();
		btnNewButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/button_cancel.png"))); // NOI18N
		btnNewButton.setText(AppLocal.getIntString("Button.Cancel")); // NOI18N
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				jcmdCancelActionPerformed(e);
			}
		});
		panel.add(btnNewButton, BorderLayout.SOUTH);
		
		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle(AppLocal.getIntString("form.productslist"));
	}


	protected void m_jEnterActionPerformed(ActionEvent evt) {
		obj = ProductSwingUtil.getProductInfo(this, dlSales1,  m_jtxtBarCode.getText());
		if (obj == null) {
			Toolkit.getDefaultToolkit().beep();
			new MessageInf(MessageInf.SGN_WARNING,
					AppLocal.getIntString("message.noproduct")).show(this);
		}else try {
			ProductUtil.setGroupProductPrice(dlSales1, obj, priceGroup);
		} catch (BasicException e) {
			obj=null;
			e.printStackTrace();
		}
			
		updateProduct();
	}

	private void jcmdCancelActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
        
    }//GEN-LAST:event_jcmdCancelActionPerformed
	
	
	 public void updateProduct() {  
		 if(obj!=null){
	        m_jRef.setText(obj.getReference());
	        m_jCode.setText(Formats.STRING.formatValue(obj.getCode()));
	        m_jName.setText(Formats.STRING.formatValue(obj.getName()));
	        m_jComment.setSelected(obj.isCom());
	        jm_origin.setText(Formats.STRING.formatValue(obj.getM_origin()));
	        jm_quantity.setText(Formats.DOUBLE.formatValue(obj.getM_quantity()));
	        jm_unit.setText(Formats.STRING.formatValue(obj.getM_unit()));
	        jm_sManufacturer.setText(Formats.STRING.formatValue(obj.getM_sManufacturer()));
	        jm_iDurability.setText(Formats.INT.formatValue(obj.getM_iDurability()));
	        jMnemonic.setText(Formats.STRING.formatValue(obj.getMnemonic()));
	        if(showBuyPrice){
	           m_jPriceBuy.setText(Formats.CURRENCY.formatValue(obj.getPriceBuy()));
	           jProductStorePanel.baseBeanChanged(obj, obj);
	        }
	        else
	           m_jPriceBuy.setText("***");
	        m_jPriceSell.setText(Formats.CURRENCY.formatValue(obj.getPriceSell()));
	        m_jCustomerPrice.setText(Formats.CURRENCY.formatValue(obj.getM_dCustomerPrice())); 
		 }else{
			    m_jRef.setText(null);
		        m_jCode.setText(null);
		        m_jName.setText(null);
		        m_jComment.setSelected(false);
		        jm_origin.setText(null);
		        jm_quantity.setText(null);
		        jm_unit.setText(null);
		        jm_sManufacturer.setText(null);
		        jm_iDurability.setText(null);
		        jMnemonic.setText(null);
		        m_jPriceBuy.setText(null);
		        m_jPriceSell.setText(null);
		        m_jCustomerPrice.setText(null);
		 }
	           
	 }
	
	
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel m_jPanEntries;
	private javax.swing.JPanel jPanel5;
	private javax.swing.JPanel jPanel9;
	private JEditorStringNumber m_jtxtBarCode;
	private javax.swing.JButton m_jEnter;
	private com.openbravo.editor.JEditorKeys m_jNumberKeys;
	private JTextField m_jName;
	private JTextField jMnemonic;
	private JTextField m_jRef;
	private JTextField m_jCode;
	private JTextField m_jPriceBuy;
	private JTextField m_jPriceSell;
	private JTextField m_jCustomerPrice;
	private JTextField jm_sManufacturer;
	private JTextField jm_origin;
	private JTextField jm_iDurability;
	private JPanel panel;
	private JButton btnNewButton;
	private JTextField jm_quantity;
	private JTextField jm_unit;
	private JCheckBox m_jComment;
	

	@Override
	protected void enterKeyProc() {
		m_jEnterActionPerformed(null);
	}
	
	@Override
	protected void escapeKeyProc() {
		jcmdCancelActionPerformed(null);
	}
	@Override
	protected void upKeyProc() {
       if(!m_jtxtBarCode.isFocusOwner())
    	   m_jtxtBarCode.activate();
	}
	@Override
	protected void downKeyProc() {
		if(!m_jtxtBarCode.isFocusOwner())
	    	   m_jtxtBarCode.activate();
	}    
}
